﻿Imports System.Windows.Forms

Public Class Preferences

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If chkKeys.CheckState = CheckState.Checked Then 'If CheckBox for Keys Info is checked
            My.Settings.ShowKeys = True 'Set Settings Variable to True
            Main.keysInfo.Visible = True 'And Show the Keys Info to user
            Main.lblKeys.Visible = True 'with the label above!
        Else
            My.Settings.ShowKeys = False 'If not, set the Settings variable to false
            Main.keysInfo.Visible = False 'And Hide the Keys Info from user
            Main.lblKeys.Visible = False
        End If

        'The Same goes below for the Table Field Types Datagrid

        If chkFieldTypes.CheckState = CheckState.Checked Then
            My.Settings.ShowTypes = True
            Main.tableInfo.Visible = True
            Main.lblInfo.Visible = True
        Else
            My.Settings.ShowTypes = False
            Main.tableInfo.Visible = False
            Main.lblInfo.Visible = False
        End If

        'Now Check if user selected to Disable Registry (ADDED 3/1/2013)
        If regSwitch.CheckState = CheckState.Checked Then 'if yes....
            My.Settings.RegistryAccessible = False
            Main.pathInUse.Text = "Using : Default Path (Registry Disabled)" 'update the status bar
        Else
            My.Settings.RegistryAccessible = True
            Main.pathInUse.Text = "Using : Registry path (Registry Active)" 'update the status bar
        End If


        Try 'This Will try to save the new path to Registry
            If My.Settings.RegistryAccessible = True Then 'If registry is accessible ...
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", qDbPath.Text) 'Update Registry Key
            End If

        Catch ex As Exception 'If Registry is not accessible, for any reason
            MsgBox("Registry is not accessible, check your Access Permissions", MsgBoxStyle.Critical) 'Notify user
        End Try

        My.Settings.pathToLogs = qDbPath.Text 'Update the RecFinder Variable pathToLogs anyway, even if registry loads normally
        Main.defaultPath = My.Settings.pathToLogs 'and pass it to defaultPath , so if registry is not accessible, this will be used to determine the path to log db 


        MsgBox("Settings Updated", MsgBoxStyle.Information) 'Notify user that everything worked ok
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles qDbPath.TextChanged

    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim logpath As String
        Try
            logDbOpen.Title = "Select DataBase File" 'The File Open Dialog Window Title
            logDbOpen.InitialDirectory = "C:/" 'Start from C:/ as default search path
            logDbOpen.Filter = "MS Access Database (*.mdb)|*.mdb|All Files (*.*)|*.*" 'A pattern for showing only mdb files (or all files)
            logDbOpen.FileName = "" 'File name must be blank first, afterwards when user selects file, it will be updated with full path
            logDbOpen.ShowDialog() 'Activate Dialog (Show)

            logpath = logDbOpen.FileName 'This Variable stores the full path for connection with the DataBase

            If logDbOpen.FileName = "" Then 'If user does not select a file to open (clicks cancel)
                Return 'Stop and get out (so no exception will occur)
            End If

            If My.Settings.RegistryAccessible = True Then


                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", logDbOpen.FileName) 'Update Registry Key
                qDbPath.Text = logDbOpen.FileName
            Else
                Main.defaultPath = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & logDbOpen.FileName 'Update def path (FIX 3/1/2013)
                qDbPath.Text = logDbOpen.FileName
            End If

        Catch ex As Exception
            MsgBox("An error occured", MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub Preferences_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        qDbPath.Text = My.Settings.pathToLogs
        If My.Settings.ShowKeys = True Then 'If Settings Variable is Set to True
            chkKeys.CheckState = CheckState.Checked 'Check the CheckBox Keys Info
        Else
            chkKeys.CheckState = CheckState.Unchecked 'Else, uncheck it
        End If

        'The Same goes for the Table Field Types Settings Variable...

        If My.Settings.ShowTypes = True Then
            chkFieldTypes.CheckState = CheckState.Checked
        Else
            chkFieldTypes.CheckState = CheckState.Unchecked
        End If

        'Check if user selected previusly to disable Registry saving..... (ADDED 3/1/2013)
        If My.Settings.RegistryAccessible = True Then 'if no
            regSwitch.CheckState = CheckState.Unchecked 'leave it unchecked
        Else
            regSwitch.CheckState = CheckState.Checked 'else check it
        End If


    End Sub

    Private Sub regCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles regCheck.Click
        'This function checks if registry is accessible or not
        Try
            If My.Settings.RegistryAccessible = True Then
                'If this message box appears, it means that registry is accessible
                MsgBox("Registry is Accessible" & vbNewLine & "Key Path = " & My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", Nothing), MsgBoxStyle.Information) 'notify that registry is accessible
            Else
                MsgBox("Registry is not accessible", MsgBoxStyle.Exclamation) 'Show a message that says no registry access <-FIX
            End If

        Catch ex As Exception
            'if exception occurs, meaning that registry is not accessble, shows the below message
            MsgBox("Registry is not accessible", MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub rstDef_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rstDef.Click
        'By  Clicking this Button, all settings revert to default ones
        qDbPath.Text = My.Settings.defaultLogPath 'Update the path with the default one
        chkFieldTypes.CheckState = CheckState.Checked 'check the field type checkbox
        chkKeys.CheckState = CheckState.Checked 'and so the keys table checkbox
        regSwitch.CheckState = CheckState.Unchecked ' <-- FIX , by default registry will be active for saving
    End Sub

End Class
